package warbots.entities.units;


import java.awt.Point;
import java.util.Hashtable;

import warbots.entities.actions.Action;
import warbots.entities.actions.ActionFactory;
import warbots.gameplay.Player;
import warbots.gameplay.exceptions.CreateUnitException;

/**
 * @author Ido Adler.
 */
public class Unit
{
    protected Player _parent;
    protected Hashtable<String,Action> _actions = new Hashtable<String,Action>();
    protected String _name;
    protected int _power;
    protected int _speed;
    protected int _health;
    protected int _price;
    protected int _direction;
    protected Point _position;
    
    /**
     * TODO: description
     * 
     * @param parent
     * @param actions
     * @param name
     * @param power
     * @param speed
     * @param health
     * @param price
     * @param direction
     * @param position
     * @throws CreateUnitException
     */
    public Unit(final Player parent, final String name, final int power, final int speed,
        final int health, final int price, final int direction, final Point position,
        final String... actions) throws CreateUnitException
    {
        if (actions.length == 0)
        {
            throw new CreateUnitException("Unit need atleast 1 action");
        }
        setName(parent.getName() + "." + name);
        _parent = parent;
        _power = power;
        _speed = speed;
        _health = health;
        _price = price;
        _direction = direction;
        _position = position;
        addActions(actions);
    }
    
    /**
     * @param p
     * 
     * @param unit
     * @param actions
     */
    public Unit(final Player p, final String unit, final String... actions)
        throws CreateUnitException
    {
    	this(p, unit, 1, 1, 1, 1, 1, new Point(0,0), actions);
    }
    
    /**
     * @return the name
     */
    public String getName()
    {
        return _name;
    }
    
    /**
     * @param name
     *            the name to set
     */
    public void setName(final String name)
    {
        _name = name;
    }
    
    /**
     * @return the health
     */
    public int getHealth()
    {
        return _health;
    }
    
    /**
     * @param health
     *            the health to set
     */
    public void setHealth(final int health)
    {
        _health = health;
    }
    
    /**
     * @return the price
     */
    public int getPrice()
    {
        return _price;
    }
    
    /**
     * @param price
     *            the price to set
     */
    public void setPrice(final int price)
    {
        _price = price;
    }
    
    /**
     * @return the actions
     */
    public String[] getActionNames()
    {
        return _actions.keySet().toArray(new String[0]);
    }
    
    /**
     * @return the power
     */
    public int getPower()
    {
        return _power;
    }
    
    /**
     * @param power
     *            the power to set
     */
    public void setPower(final int power)
    {
        _power = power;
    }
    
    /**
     * @return the speed
     */
    public int getSpeed()
    {
        return _speed;
    }
    
    /**
     * @param speed
     *            the speed to set
     */
    public void setSpeed(final int speed)
    {
        _speed = speed;
    }
    
    /**
     * @return the direction
     */
    public int getDirection()
    {
        return _direction;
    }
    
    /**
     * @param direction
     *            the direction to set
     */
    public void setDirection(final int direction)
    {
        _direction = direction;
    }
    
    /**
     * @return the position
     */
    public Point getPosition()
    {
        return _position;
    }
    
    /**
     * @param position
     *            the position to set
     */
    public void setPosition(final Point position)
    {
        _position = position;
    }
    
    public boolean isAlive()
    {
        return _health > 0;
    }
    
    /**
     * TODO: description
     * 
     * @param actions
     */
    protected void addActions(final String[] actions)
    {
        for (String action : actions)
        {
        	Action current = ActionFactory.newInstance(this, action);
            _actions.put(current.getName(),current);
            _price+=current.getPrice();
        }
    }
    
    /**
     * TODO: description
     * 
     * @param hitAction
     */
    public void getHit(final Action action)
    {
        _health -= action.getDamage();
        if (_health < 0)
            goDie();
    }
    
    /**
     * TODO: description
     */
    private void goDie()
    {
        _parent.removeUnit(this);
    }

	public Action chooseAction(String key) {
		// TODO Auto-generated method stub
		return _actions.get(key);
	}
    
}
